home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Source.bin / VetoableChangeSupport.java < prev    next >
Text File  |  1998-08-21  |  6KB  |  184 lines

  1. /*
  2.  * @(#)VetoableChangeSupport.java    1.0 5/6/97
  3.  *
  4.  * Copyright (c) 1997 Symantec, Inc. All Rights Reserved.
  5.  *
  6.  */
  7.  
  8. package symantec.itools.beans;
  9.  
  10. import java.io.Serializable;
  11. import java.io.ObjectOutputStream;
  12. import java.io.ObjectInputStream;
  13. import java.io.IOException;
  14. import java.beans.PropertyVetoException;
  15. import java.beans.PropertyChangeListener;
  16. import java.beans.VetoableChangeListener;
  17. import java.beans.PropertyChangeEvent;
  18.  
  19.  
  20.  
  21. //    05/06/97    LAB    Created
  22.  
  23. /**
  24.  * This is a utility class that can be used by beans that support constrained
  25.  * properties.  Your can either inherit from this class or you can use
  26.  * an instance of this class as a member field of your bean and delegate
  27.  * various work to it.
  28.  * <p>
  29.  * This extension of the java.beans.VetoableChangeSupport class adds
  30.  * functionality to handle individual property changes.
  31.  *
  32.  * @author Symantec
  33.  */
  34. public class VetoableChangeSupport extends java.beans.VetoableChangeSupport implements java.io.Serializable
  35. {
  36.  
  37.     /**
  38.      * Constructs a VetoableChangeSupport object.
  39.      * @param sourceBean the bean to be given as the source for any events
  40.      */
  41.  
  42.     public VetoableChangeSupport(Object sourceBean)
  43.     {
  44.         super(sourceBean);
  45.         source = sourceBean;
  46.     }
  47.  
  48.     /**
  49.      * Adds a VetoableListener to the listener list.
  50.      *
  51.      * @param propertyName    the name of the property to add a listener for
  52.      * @param listener  the VetoableChangeListener to be added
  53.      * @see #removeVetoableChangeListener
  54.      */
  55.     public synchronized void addVetoableChangeListener(String propertyName, VetoableChangeListener listener)
  56.     {
  57.         java.util.Vector listenerList;
  58.  
  59.         if(listenerTable == null)
  60.         {
  61.             listenerTable = new java.util.Hashtable();
  62.         }
  63.  
  64.         if(listenerTable.containsKey(propertyName))
  65.         {
  66.             listenerList = (java.util.Vector)listenerTable.get(propertyName);
  67.         }
  68.         else
  69.         {
  70.             listenerList = new java.util.Vector();
  71.         }
  72.  
  73.         listenerList.addElement(listener);
  74.         listenerTable.put(propertyName, listenerList);
  75.     }
  76.  
  77.     /**
  78.      * Removes a VetoableChangeListener from the listener list.
  79.      *
  80.      * @param propertyName    the name of the property to remove a listener for.
  81.      * @param listener    the VetoableChangeListener to be removed
  82.      * @see #addVetoableChangeListener
  83.      */
  84.     public synchronized void removeVetoableChangeListener(String propertyName, VetoableChangeListener listener)
  85.     {
  86.         java.util.Vector listenerList;
  87.  
  88.         if (listenerTable == null || !listenerTable.containsKey(propertyName))
  89.         {
  90.             return;
  91.         }
  92.         listenerList = (java.util.Vector)listenerTable.get(propertyName);
  93.         listenerList.removeElement(listener);
  94.     }
  95.  
  96.     /**
  97.      * Reports a vetoable property update to any registered listeners.
  98.      * If anyone vetos the change, then a new event is fired reverting everyone to
  99.      * the old value, and then the PropertyVetoException is rethrown.
  100.      * <p>
  101.      * No event is fired if old and new are equal and non-null.
  102.      *
  103.      * @param propertyName  the programmatic name of the property
  104.      *        that was changed
  105.      * @param oldValue  the old value of the property
  106.      * @param newValue  the new value of the property
  107.      *
  108.      * @exception PropertyVetoException
  109.      * if the specified property value is unacceptable
  110.      */
  111.     public void fireVetoableChange(String propertyName, Object oldValue, Object newValue) throws PropertyVetoException
  112.     {
  113.         if (oldValue != null && oldValue.equals(newValue))
  114.         {
  115.             return;
  116.         }
  117.  
  118.         super.fireVetoableChange(propertyName, oldValue, newValue);
  119.  
  120.         java.util.Hashtable templistenerTable = null;
  121.  
  122.         synchronized (this)
  123.         {
  124.             if(listenerTable == null || !listenerTable.containsKey(propertyName))
  125.             {
  126.                 return;
  127.             }
  128.               templistenerTable = (java.util.Hashtable) listenerTable.clone();
  129.         }
  130.  
  131.         java.util.Vector listenerList;
  132.  
  133.         listenerList = (java.util.Vector)templistenerTable.get(propertyName);
  134.  
  135.         PropertyChangeEvent evt = new PropertyChangeEvent(source, propertyName, oldValue, newValue);
  136.  
  137.         try
  138.         {
  139.             for (int i = 0; i < listenerList.size(); i++)
  140.             {
  141.                 VetoableChangeListener target = (VetoableChangeListener)listenerList.elementAt(i);
  142.                 target.vetoableChange(evt);
  143.             }
  144.         }
  145.         catch (PropertyVetoException veto)
  146.         {
  147.             // Create an event to revert everyone to the old value.
  148.                evt = new PropertyChangeEvent(source, propertyName, newValue, oldValue);
  149.             for (int i = 0; i < listenerList.size(); i++)
  150.             {
  151.                 try
  152.                 {
  153.                     VetoableChangeListener target = (VetoableChangeListener)listenerList.elementAt(i);
  154.                     target.vetoableChange(evt);
  155.                 }
  156.                 catch (PropertyVetoException ex)
  157.                 {
  158.                      // We just ignore exceptions that occur during reversions.
  159.                 }
  160.             }
  161.             // And now rethrow the PropertyVetoException.
  162.             throw veto;
  163.         }
  164.     }
  165.  
  166.     /* !!! LAB !!!    05/06/97
  167.     If we want to support non-serializable listeners we will have to
  168.     implement the folowing functions and serialize out the listenerTable
  169.     HashTable on our own.
  170.  
  171.     private void writeObject(ObjectOutputStream s) throws IOException
  172.     private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException
  173.     */
  174.  
  175.     /**
  176.      * The listener table.
  177.      * @see #addVetoableChangeListener
  178.      * @see #removeVetoableChangeListener
  179.      */
  180.     protected java.util.Hashtable listenerTable;
  181.     private Object source;
  182.     private int vetoableChangeSupportSerializedDataVersion = 1;
  183. }
  184.